package com.example.dell.widgettest_225zxx;

import android.content.Intent;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ButtonBarLayout;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private TextView tv;
    private EditText et;
    private Button btn1,btn2;
    private ImageButton ibtn;
    private ImageView iv;

    private RadioButton rbnan,rbnv;
    private RadioGroup rg;
    private CheckBox cblan,cbzu,cbyou;
    private Button btngetgender,btngerfun;

    private String strfun="";//保存兴趣的字符串

    public MainActivity() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        tv=(TextView)findViewById(R.id.textView);
        et=(EditText)findViewById(R.id.editText);
        btn1=(Button)findViewById(R.id.button);
        btn2=(Button)findViewById(R.id.button2);
        ibtn=(ImageButton)findViewById(R.id.imageButton3);
        iv=(ImageView)findViewById(R.id.imageView);
        //获取8个控件
        rbnan=(RadioButton)findViewById(R.id.radioButton2);
        rbnv=(RadioButton)findViewById(R.id.radioButton);
        rg=(RadioGroup)findViewById(R.id.radiogroup);
        cblan=(CheckBox)findViewById(R.id.checkBox);
        cbzu=(CheckBox)findViewById(R.id.checkBox2);
        cbyou=(CheckBox)findViewById(R.id.checkBox3);
        btngetgender=(Button)findViewById(R.id.button4);
        btngerfun=(Button)findViewById(R.id.button5);

        btngetgender.setOnClickListener(this);
        btngerfun.setOnClickListener(this);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        ibtn.setOnClickListener(this);

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override 
            public void onCheckedChanged(RadioGroup group, @IdRes int i) {
                switch (i)
                {
                    case R.id.radioButton://女
                        tv.setText("女");
                        break;
                    case R.id.radioButton2://男
                        tv.setText("男");
                        break;
                }
            }
        });
        cblan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)//CheckBox被勾选
                strfun+="篮球+";
                else //checkBox未被勾选
                strfun=strfun.replace("篮球+","");
                        tv.setText(strfun);
            }
        });
        //足球选择改变事件
        cbzu.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked)
                    strfun+="足球+";
                else
                    strfun=strfun.replace("足球+","");
                tv.setText(strfun);
            }
        });
        //游泳选择改变事件
        cbyou.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked)
                    strfun+="游泳+";
                else
                    strfun=strfun.replace("游泳+","");
                tv.setText(strfun);
            }
        });
    }

    @Override
    public void onClick(View v) {
            switch (v.getId())
            {
                case R.id.button://获取文本按钮
                    String str=et.getText().toString();
                    tv.setText(str);
                    break;
                case R.id.button2://设置图片
                    iv.setImageResource(R.drawable.haha);
                    break;
                case R.id.imageButton3://图片按钮
                    Intent intent=new Intent(MainActivity.this,NewActivity.class);
                    intent.putExtra("param1",et.getText().toString());
                    intent.putExtra("param2",3);
                    startActivity(intent);
                    break;
                case R.id.button4://获取性别
                    if(rbnan.isChecked())
                        tv.setText("男");
                    if(rbnv.isChecked())
                        tv.setText("女");
                    break;
                case R.id.button5://获取兴趣
                    String msg="";
                    if(cblan.isChecked())   msg+="篮球+";
                    if(cbzu.isChecked())    msg+="足球+";
                    if(cbyou.isChecked())   msg+="游泳+";
                    tv.setText(msg);
                    break;
            }

    }
}
